package com.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crm.base.PageResult;
import com.crm.domain.dto.ClueDto;
import com.crm.domain.entity.Business;
import com.crm.domain.entity.Clue;
import com.crm.domain.vo.ClueVo;
import com.crm.mapper.ClueMapper;
import com.crm.service.ClueService;
import com.crm.utils.HttpStatus;
import com.crm.utils.ObjectUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;


@Service
@Slf4j
@RequiredArgsConstructor
public class ClueServiceImpl extends ServiceImpl<ClueMapper, Clue> implements ClueService {
    private final ClueMapper clueMapper;

    /**
     * 分页查询线索列表
     *
     * @param clueDto
     * @return
     */
    @Override
    public PageResult<ClueVo> queryAll(ClueDto clueDto) {
        //设置分页参数
        Page<Clue> page = Page.of(clueDto.getPageNum(), clueDto.getPageSize());
        // 构建查询条件
        LambdaQueryWrapper<Clue> queryWrapper = Wrappers.<Clue>lambdaQuery()
                .eq(clueDto.getId() != null, Clue::getId, clueDto.getId())
                .eq(clueDto.getChannel() != null, Clue::getChannel, clueDto.getChannel())
                .like(clueDto.getPhone() != null, Clue::getPhone, clueDto.getPhone())
                .like(clueDto.getOwner() != null, Clue::getOwner, clueDto.getOwner())
                .like(clueDto.getStatus() != null, Clue::getStatus, clueDto.getStatus())
                .ge(clueDto.getCreateTime() != null, Clue::getCreateTime, clueDto.getCreateTime())
                .le(clueDto.getUpdateTime() != null, Clue::getUpdateTime, clueDto.getUpdateTime());

        // 执行分页查询
        Page<Clue> cluePage = clueMapper.selectPage(page, queryWrapper);

        // 结果转换
        List<ClueVo> clueVoList = cluePage.getRecords().stream()
                .map(clue -> {
                    ClueVo clueVo = new ClueVo();
                    BeanUtils.copyProperties(clue, clueVo);
                    return clueVo;
                })
                .collect(Collectors.toList());

        // 构建分页响应
        PageResult<ClueVo> pageResult = new PageResult<>();
        pageResult.setTotal(clueVoList.size());
        pageResult.setRows(clueVoList);
        pageResult.setMsg("查询成功");
        pageResult.setCode(HttpStatus.SUCCESS);

        return pageResult;
    }

    @Override
    public PageResult<ClueVo> queryPool(ClueDto clueDto) {
        //设置分页参数
        Page<Clue> page = Page.of(clueDto.getPageNum(), clueDto.getPageSize());
        // 构建查询条件
        LambdaQueryWrapper<Clue> queryWrapper = Wrappers.<Clue>lambdaQuery()
                .eq(clueDto.getId() != null, Clue::getId, clueDto.getId())
                .eq(clueDto.getChannel() != null, Clue::getChannel, clueDto.getChannel())
                .like(clueDto.getPhone() != null, Clue::getPhone, clueDto.getPhone())
                .ge(clueDto.getCreateTime() != null, Clue::getCreateTime, clueDto.getCreateTime())
                .le(clueDto.getUpdateTime() != null, Clue::getUpdateTime, clueDto.getUpdateTime());
        // 执行分页查询
        Page<Clue> cluePage = clueMapper.selectPage(page, queryWrapper);

        // 结果转换
        List<ClueVo> clueVoList = cluePage.getRecords().stream()
                .map(clue -> {
                    ClueVo clueVo = new ClueVo();
                    BeanUtils.copyProperties(clue, clueVo);
                    return clueVo;
                })
                .collect(Collectors.toList());

        // 构建分页响应
        PageResult<ClueVo> pageResult = new PageResult<>();
        pageResult.setTotal(clueVoList.size());
        pageResult.setRows(clueVoList);
        pageResult.setMsg("查询成功");
        pageResult.setCode(HttpStatus.SUCCESS);

        return pageResult;
    }

    /**
     * 添加到线索池
     * @param clue
     */
    @Override
    public void addInfo(Clue clue) {
        clueMapper.insert(clue);
    }

    /**
     * 转为伪线索
     * @param clueId
     * @param clueDto
     */
    @Override
    public void setFalseClue(Long clueId, ClueDto clueDto) {
        clueMapper.setFalseClueById(clueId);
    }
}
